/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef DRIVERS_I2C_H
#define DRIVERS_I2C_H

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>

namespace DRIVER_I2C {

// I2C读取参数结构体
struct I2C_READ_S {
    uint8_t  rv_id;        // 总线ID
    uint32_t length;       // 读取长度
    uint32_t time_out_cnt; // 超时时间
    uint32_t re_read_cnt;  // 重读次数
};

// I2C驱动类
class I2c {
public:
    I2c() : m_offset_width(1) {
    } // 默认偏移量宽度为1
    ~I2c() = default;

    // 设置偏移量宽度
    void set_offset_width(uint8_t offset_width) {
        m_offset_width = offset_width;
    }

    // 获取偏移量宽度
    uint8_t get_offset_width() const {
        return m_offset_width;
    }

    // 初始化I2C驱动
    void init(uint8_t id, uint32_t speed, uint8_t mode, uint8_t slave_addr);

    // 锁定总线
    void lock();

    // 解锁总线
    void unlock();

    // 读取数据
    std::tuple<int, std::optional<std::string>> read(const I2C_READ_S* read_data,
                                                     std::string_view  tx_buf);

    // 写入数据
    int write(uint8_t id, std::string_view tx_buf, uint32_t timeout_ms, uint32_t flags);

private:
    uint8_t m_offset_width; // 偏移量宽度
    uint8_t m_bus_id;       // 总线ID
};

} // namespace DRIVER_I2C

#endif // DRIVERS_I2C_H